// 引入数据库池
const pool = require('../pool.js');
// 创建订单路由
const express = require('express');
const { createPool } = require('mysql');
const r = express.Router();

// 添加用户订单
// http://127.0.0.1:8888/v1/indent/add
r.post("/add",(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query('insert into hqyc_indent set ?',[obj],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    // console.log(result.length);
    result.affectedRows==1?res.send({code:1,msg:'用户订单添加成功',data:result}):res.send({code:0,msg:'用户订单添加失败',data:result});
  })
})

// 购买优速通
// http://127.0.0.1:8888/v1/indent/upust
r.post("/upust",(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query('update hqyc_indent set kstd=? where userid = ?',[obj.kstd,obj.userid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    console.log(result);
    // 0未购买 1一次性 2无限次
    result.affectedRows==1?res.send({code:1,msg:'优速通购买成功',data:result}):res.send({code:0,msg:'优速通购买失败',data:result});
  })
})

// 修改订单状态
// http://127.0.0.1:8888/v1/indent/upstatu
r.post("/upstatu",(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query('update hqyc_indent set statu=? where indentid=?',[obj.statu,obj.indentid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    // console.log(result);
    // 0待支付 1待使用（已付款） 2取消 3退款
    result.affectedRows>0?res.send({code:1,msg:'修改订单状态成功',data:result}):res.send({code:0,msg:'修改订单状态失败',data:result});
  })
})

// 删除订单
// http://127.0.0.1:8888/v1/indent/del
r.post("/del",(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query('delete from hqyc_indent where id=? and userid=?',[obj.id,obj.userid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    // console.log(result);
    // 0待支付 1待使用（已付款） 2取消 3退款
    result.affectedRows==1?res.send({code:1,msg:'删除订单成功',data:result}):res.send({code:0,msg:'删除订单失败',data:result});
  })
})

// 查询用户订单列表
// http://127.0.0.1:8888/v1/indent/query
r.post("/query",(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query('select *,count(visinfo) as cou from hqyc_indent where userid=? group by indentid',[obj.userid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    // console.log(result.length);
    result.length>0?res.send({code:1,msg:'用户订单查询成功',data:result}):res.send({code:0,msg:'用户订单查询失败',data:result});
  })
})


// 查询用户订单详情
// http://127.0.0.1:8888/v1/indent/details
r.post("/details",(req,res,next)=>{
  let obj = req.body;
  // console.log(obj);
  pool.query('select * from hqyc_indent where indentid=? and userid=?',[obj.indentid,obj.userid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    // console.log(result.length);
    result.length>0?res.send({code:1,msg:'用户订单查询成功',data:result}):res.send({code:0,msg:'用户订单查询失败',data:result});
  })
})

//查询首页模块数据
// http://127.0.0.1:8888/v1/indent/play
r.get('/play',(req,res,next)=>{
  let obj = req.query;
  //console.log(obj);
  pool.query('select * from hqyc_item where item_category_id=?',[obj.cid],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    res.send({
      code:200,
      msg:'ok',
      data:result
    })
  })
})

//查询主题模块数据
// http://127.0.0.1:8888/v1/indent/topic
r.get('/topic',(req,res,next)=>{
  pool.query('select * from hqyc_topic',(err,result)=>{
    if(err){
      next(err);
      return;
    }
    res.send({
      code:200,
      msg:'ok',
      data:result
    })
  })
})

module.exports = r;